<html>
<head><meta charset="utf-8"><title>Why are trait objects unsized if the layout is known? · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20are.20trait.20objects.20unsized.20if.20the.20layout.20is.20known.3F.html">Why are trait objects unsized if the layout is known?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="223085178"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20are%20trait%20objects%20unsized%20if%20the%20layout%20is%20known%3F/near/223085178" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20are.20trait.20objects.20unsized.20if.20the.20layout.20is.20known.3F.html#223085178">(Jan 18 2021 at 06:37)</a>:</h4>
<p>A trait object (as described by <code>core::raw::TraitObject</code>) is just two pointers, and pointers have known size. Why are trait objects not <code>Sized</code> then? For example, why can I not do (using current master branch)</p>
<div class="codehilite"><pre><span></span><code>let x: [u8; size_of::&lt;dyn Debug&gt;()];
</code></pre></div>
<p>which fails with <code>the size for values of type </code>dyn Debug<code> cannot be known at compilation time</code>? I think the size of this should be <code>2 * size_of&lt;*mut ()&gt;</code>.</p>



<a name="223086149"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20are%20trait%20objects%20unsized%20if%20the%20layout%20is%20known%3F/near/223086149" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20are.20trait.20objects.20unsized.20if.20the.20layout.20is.20known.3F.html#223086149">(Jan 18 2021 at 06:49)</a>:</h4>
<p><span class="user-mention" data-user-id="119169">@osa1</span> &amp;dyn Debug is two pointers (also known as a "fat pointer"), not dyn Debug itself</p>



<a name="223086361"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20are%20trait%20objects%20unsized%20if%20the%20layout%20is%20known%3F/near/223086361" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20are.20trait.20objects.20unsized.20if.20the.20layout.20is.20known.3F.html#223086361">(Jan 18 2021 at 06:54)</a>:</h4>
<p>That makes sense, thanks <span class="user-mention" data-user-id="232545">@Joshua Nelson</span> .</p>
<p>Relatedly, I'm wondering how size of a reference type is computed in the compiler. Is there a function somewhere (maybe in miri?) where given a reference type (<code>&amp;X</code> where <code>X</code> is a struct, trait, or a slice like <code>[u8]</code>, or something else) it returns the size?</p>



<a name="223086377"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20are%20trait%20objects%20unsized%20if%20the%20layout%20is%20known%3F/near/223086377" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20are.20trait.20objects.20unsized.20if.20the.20layout.20is.20known.3F.html#223086377">(Jan 18 2021 at 06:54)</a>:</h4>
<p>I think normal <code>std::mem::size_of</code> will tell you</p>



<a name="223086383"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20are%20trait%20objects%20unsized%20if%20the%20layout%20is%20known%3F/near/223086383" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20are.20trait.20objects.20unsized.20if.20the.20layout.20is.20known.3F.html#223086383">(Jan 18 2021 at 06:55)</a>:</h4>
<p>Not sure how it's implemented, probably an intrinsic</p>



<a name="223086397"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20are%20trait%20objects%20unsized%20if%20the%20layout%20is%20known%3F/near/223086397" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20are.20trait.20objects.20unsized.20if.20the.20layout.20is.20known.3F.html#223086397">(Jan 18 2021 at 06:55)</a>:</h4>
<p>Yeah it's an intrinsic. I'll find the definition of it. Thanks.</p>



<a name="223112773"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20are%20trait%20objects%20unsized%20if%20the%20layout%20is%20known%3F/near/223112773" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20are.20trait.20objects.20unsized.20if.20the.20layout.20is.20known.3F.html#223112773">(Jan 18 2021 at 12:20)</a>:</h4>
<p><code>ty/layout.rs</code> probably contains the interesting bits</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>